package com.chinags.system.report.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.chinags.common.core.domain.Ztree;
import com.chinags.common.exception.BusinessException;
import com.chinags.common.utils.StringUtils;
import com.chinags.system.report.domain.TJcFlzddw;
import com.chinags.system.report.mapper.TJcFlzddwMapper;
import com.chinags.system.report.service.ITJcFlzddwService;
import com.chinags.system.testingfacility.domain.TJcJcjg;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.chinags.common.core.text.Convert;
import org.springframework.transaction.annotation.Transactional;
/**
 * 防雷重点单位 服务层实现
 * 
 * @author liut
 * @date 2019-06-20
 */
@Service
public class TJcFlzddwServiceImpl implements ITJcFlzddwService
{
	private static final Logger log = LoggerFactory.getLogger(TJcFlzddwServiceImpl.class);
	@Autowired
	private TJcFlzddwMapper tJcFlzddwMapper;

	@Autowired
    private ITJcFlzddwService itJcFlzddwService;


	/**
     * 查询防雷重点单位信息
     * 
     * @param thunderUnitId 防雷重点单位ID
     * @return 防雷重点单位信息
     */
    @Override
	public TJcFlzddw selectTJcFlzddwById(Long thunderUnitId)
	{
	    return tJcFlzddwMapper.selectTJcFlzddwById(thunderUnitId);
	}
	
	/**
     * 查询防雷重点单位列表
     * 
     * @param tJcFlzddw 防雷重点单位信息
     * @return 防雷重点单位集合
     */
	@Override
	public List<TJcFlzddw> selectTJcFlzddwList(TJcFlzddw tJcFlzddw)
	{


	    return tJcFlzddwMapper.selectTJcFlzddwListflz(tJcFlzddw);
	}
	
    /**
     * 新增防雷重点单位
     * 
     * @param tJcFlzddw 防雷重点单位信息
     * @return 结果
     */
	@Override
	@Transactional
	public int insertTJcFlzddw(TJcFlzddw tJcFlzddw)
	{

		String tjcFlzddwName =  tJcFlzddw.getUnitName();
		Long creditCode =tJcFlzddw.getCreditCode();
		List<Long> credList = tJcFlzddwMapper.selectCredit();
		for(Long list:credList){
			if(list == creditCode){
				return -1;
			}
		}

		if(tjcFlzddwName == "" || creditCode == null  ){
			return -2;
		}


	    return tJcFlzddwMapper.insertTJcFlzddw(tJcFlzddw);
	}
	
	/**
     * 修改防雷重点单位
     * 
     * @param tJcFlzddw 防雷重点单位信息
     * @return 结果
     */
	@Override
	public int updateTJcFlzddw(TJcFlzddw tJcFlzddw)
	{
	    return tJcFlzddwMapper.updateTJcFlzddw(tJcFlzddw);
	}



	/**
     * 删除防雷重点单位对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
	@Override
	@Transactional
	public int deleteTJcFlzddwByIds(String ids)
	{
		return tJcFlzddwMapper.deleteTJcFlzddwByIds(Convert.toStrArray(ids));
	}

	@Override
	public String importUser(List<TJcFlzddw> tJcFlzddwList, boolean updateSupport, String operName) {
		if (StringUtils.isNull(tJcFlzddwList) || tJcFlzddwList.size() == 0)
		{
			throw new BusinessException("导入用户数据不能为空！");

		}

		int successNum = 0;
		int failureNum = 0;
		StringBuilder successMsg = new StringBuilder();
		StringBuilder failureMsg = new StringBuilder();
//		String password = configService.selectConfigByKey("sys.tjcFlzddw.initPassword");

		for (TJcFlzddw tJcFlzddw : tJcFlzddwList)
		{
			try
			{
				// 验证是否存在这个单位
				TJcFlzddw t = tJcFlzddwMapper.selectFlzddwByLoginName(tJcFlzddw.getCreditCode());
				if (StringUtils.isNull(t))
				{
                    tJcFlzddw.setCreateBy(operName);
					this.insertTJcFlzddw(tJcFlzddw);
					successNum++;
					successMsg.append("<br/>" + successNum + "、 单位" + tJcFlzddw.getUnitName() + " 导入成功");
				}
				else if (updateSupport)
				{
                    tJcFlzddw.setUpdateBy(operName);
					this.updateTJcFlzddw(tJcFlzddw);
					successNum++;
					successMsg.append("<br/>" + successNum + "、单位 " + tJcFlzddw.getUnitName() + " 更新成功");
				}
				else
				{
					failureNum++;
					failureMsg.append("<br/>" + failureNum + "、单位 " + tJcFlzddw.getUnitName() + " 已存在");
				}
			}
			catch (Exception e)
			{
				failureNum++;
				String msg = "<br/>" + failureNum + "、单位 " + tJcFlzddw.getUnitName() + " 导入失败：";
				failureMsg.append(msg + e.getMessage());
				log.error(msg, e);
			}
		}
		if (failureNum > 0)
		{
			failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
			throw new BusinessException(failureMsg.toString());
		}
		else
		{
			successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
		}
		return successMsg.toString();

	}

	@Override
	public List<Map<String,TJcFlzddw>> check() {
		return tJcFlzddwMapper.checkCreade();
	}

	@Override
	public TJcFlzddw selectJobLogById(Long thunderUnitId) {
		return tJcFlzddwMapper.selectJobLogById(thunderUnitId);
	}

	@Override
	public int insertTJcFlzddwto(TJcFlzddw tJcFlzddw) {
		String tjcFlzddwName =  tJcFlzddw.getUnitName();
		Long creditCode =tJcFlzddw.getCreditCode();
		List<Long> credList = tJcFlzddwMapper.selectCredit();
		for(Long list:credList){
			if(list == creditCode){
				return -1;
			}
		}

		if(tjcFlzddwName == ""   ){
			return -2;
		}else if(creditCode == null){
			return -3;
		}
		return tJcFlzddwMapper.insertTJcFlzddw(tJcFlzddw);
	}

	@Override
	public int selectCreadConde(Long creditCode) {
		List<Long> creditList =  tJcFlzddwMapper.selectCredit();
		for(Long list : creditList){
			if(list == creditCode){
				return -4;
			}

		}
		return 0;
	}
	/**
     * 根据社会统一代码查询信息
     *
     * */

    @Override
    public TJcFlzddw selectByUnitName(Long creditCode) {

        return tJcFlzddwMapper.queryByUnitName(creditCode);
    }

	@Override
	public List<TJcJcjg> selectTJcJcjgList(TJcJcjg tJcJcjg) {
		return tJcFlzddwMapper.selectTJcJcjgList(tJcJcjg);
	}

//	@Override
//	public TJcFlzddw selectName() {
//		return tJcFlzddwMapper.selectByName();
//	}
	/**
	 * 查询所有菜单
	 *
	 * @return 菜单列表
	 */
	/*@Override
	public List<Ztree> menuTreeData(Long userId)
	{
		List<TJcFlzddw> menuList = selectMenuAll(userId);
		List<Ztree> ztrees = initZtree(menuList);
		return ztrees;
	}*/
	/**
	 * 查询菜单集合
	 *
	 * @return 所有菜单信息
	 */
	/*@Override
	public List<TJcFlzddw> selectMenuAll(Long userId)
	{
		List<TJcFlzddw> menuList = null;

		if (SysUser.is(userId))
		{
			menuList = tJcFlzddwMapper.selectMenuAll();
		}
		else
		{
			menuList = tJcFlzddwMapper.selectMenuAllByUserId(userId);
		}
		return menuList;
	}
	*//**
	 * 对象转菜单树
	 *
	 * @param menuList 菜单列表
	 * @return 树结构列表
	 *//*
	public List<Ztree> initZtree(List<TJcFlzddw> menuList)
	{
		return initZtree(menuList, null, false);
	}
	*//**
	 * 对象转菜单树
	 *
	 * @param menuList 菜单列表
	 * @param roleMenuList 角色已存在菜单列表
	 * @param permsFlag 是否需要显示权限标识
	 * @return 树结构列表
	 *//*
	public List<Ztree> initZtree(List<TJcFlzddw> menuList, List<String> roleMenuList, boolean permsFlag)
	{
		List<Ztree> ztrees = new ArrayList<Ztree>();
		boolean isCheck = StringUtils.isNotNull(roleMenuList);
		for (TJcFlzddw menu : menuList)
		{
			Ztree ztree = new Ztree();
			ztree.setId(menu.getCreateId());
			ztree.setName(transMenuName(menu, roleMenuList, permsFlag));
			ztree.setTitle(menu.getMenuName());
			if (isCheck)
			{
				ztree.setChecked(roleMenuList.contains(menu.getMenuId() + menu.getPerms()));
			}
			ztrees.add(ztree);
		}
		return ztrees;
	}
	public String transMenuName(TJcFlzddw menu, List<String> roleMenuList, boolean permsFlag)
	{
		StringBuffer sb = new StringBuffer();
		sb.append(menu.getUnitName());
		if (permsFlag)
		{
			sb.append("<font color=\"#888\">&nbsp;&nbsp;&nbsp;" + menu.getPerms() + "</font>");
		}
		return sb.toString();
	}*/

//	@Override
////	@DataScope(tableAlias = "d")
//	public List<Ztree> selecitDeptTree(TJcFlzddwCount dept) {
//		List<TJcFlzddwCount> deptList = TJcFlzddwMapper.selectDeptList(dept);
//		List<Ztree> ztrees = initZtree(deptList);
//		return ztrees;
//	}
//	/**
//	 * 对象转部门树
//	 *
//	 * @param deptList 部门列表
//	 * @return 树结构列表
//	 */
//	public List<Ztree> initZtree(List<TJcFlzddwCount> deptList)
//	{
//		return initZtree(deptList, null);
//	}
//	/**
//	 * 对象转部门树
//	 *
//	 * @param deptList 部门列表
//	 * @param roleDeptList 角色已存在菜单列表
//	 * @return 树结构列表
//	 */
//	public List<Ztree> initZtree(List<TJcFlzddwCount> deptList, List<String> roleDeptList)
//	{
//
//		List<Ztree> ztrees = new ArrayList<Ztree>();
//		boolean isCheck = StringUtils.isNotNull(roleDeptList);
//		for (TJcFlzddwCount dept : deptList)
//		{
//			if (UserConstants.DEPT_NORMAL.equals(dept.getStatus()))
//			{
//				Ztree ztree = new Ztree();
//				ztree.setId(dept.getCreateId());
//				ztree.setpId(dept.getPrentId());
//				ztree.setName(dept.getName());
//				ztree.setTitle(dept.getName());
//				if (isCheck)
//				{
//					ztree.setChecked(roleDeptList.contains(dept.getCreateId() + dept.getName()));
//				}
//				ztrees.add(ztree);
//			}
//		}
//		return ztrees;
//	}
//



}
